package edu.example.Absence.servlet;
import edu.example.common.PowerCheck;

import edu.example.Absence.Dao.absenceDao;
import edu.example.Absence.bean.AbsenceVo;
import edu.example.Absence.bean.absence;
import edu.example.house.dao.housemanageDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/absence")
public class AbsenceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<absence> list = new absenceDao().getList();

        List<AbsenceVo> res = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        for (absence absence : list) {
            AbsenceVo absenceVo = new AbsenceVo();
            absenceVo.setId(absence.getId());
            absenceVo.setBuilding_card(absence.getBuilding_card());
            absenceVo.setGender(absence.getGender());
            absenceVo.setName(absence.getName());
            absenceVo.setRooms_num(absence.getRoom_num());
            absenceVo.setUser_id(absence.getUser_id());

            absenceVo.setLessdate(simpleDateFormat.format(new Date(absence.getLessdate())));

            res.add(absenceVo);
        }
        req.setAttribute("list", res);
        req.getRequestDispatcher("/absences/absence.jsp").forward(req,resp);

        req.getRequestDispatcher("absence.jsp").forward(req, resp);
    }
}